package cn.sxy.mybatis.session.defaults;

import cn.sxy.mybatis.binding.MapperRegistry;
import cn.sxy.mybatis.executor.Executor;
import cn.sxy.mybatis.mapping.BoundSql;
import cn.sxy.mybatis.mapping.Environment;
import cn.sxy.mybatis.mapping.MappedStatement;
import cn.sxy.mybatis.session.Configuration;
import cn.sxy.mybatis.session.RowBounds;
import cn.sxy.mybatis.session.SqlSession;


import java.lang.reflect.Method;
import java.sql.*;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

public class DefaultSqlSession implements SqlSession {

    private final Configuration configuration;

    private final Executor executor;

    public DefaultSqlSession(Configuration configuration, Executor executor) {
        this.configuration = configuration;
        this.executor = executor;
    }


    // 省略构造函数


    @Override
    public <T> T selectOne(String statement) {
//        return (T) ("你被代理了"+statement);
        return this.selectOne(statement, null);
    }


    @Override
    public <T> T selectOne(String statement, Object parameter) {
//        MappedStatement mappedStatement = configuration.getMappedStatement(statement);
//        return (T) ("你被代理了！" + "\n方法：" + statement + "\n入参：" + parameter + "\n待执行SQL：" + mappedStatement.getSql());

//        try{
//            MappedStatement mappedStatement = configuration.getMappedStatement(statement);
//            Environment environment = configuration.getEnvironment();
//
//            System.out.println("准备获取连接...");
//            Connection connection = environment.getDataSource().getConnection();
//            System.out.println("连接获取成功！");
////            Connection connection=environment.getDataSource().getConnection();
//
//            BoundSql boundSql = mappedStatement.getBoundSql();
//            System.out.println("SQL语句：" + boundSql.getSql());
//            System.out.println("参数映射：" + boundSql.getParameterMappings());
//            System.out.println("参数值：" + ((Object[]) parameter)[0]);
//
//            PreparedStatement preparedStatement = connection.prepareStatement(boundSql.getSql());
//            //设置参数 id
//            preparedStatement.setLong(1, Long.parseLong(((Object[]) parameter)[0].toString()));
//            ResultSet resultSet = preparedStatement.executeQuery();
//            //执行查询
//            List<T> objList = (List<T>) resultSet2Obj(resultSet, Class.forName(boundSql.getResultType()));
//            return objList.get(0);
//
//        } catch (Exception e) {
//            e.printStackTrace();
//            return null;
//
//        }

        MappedStatement ms = configuration.getMappedStatement(statement);
//        List<T> list = executor.query(ms, parameter, Executor.NO_RESULT_HANDLER, ms.getSqlSource().getBoundSql(parameter));
//        List<T> list = executor.query(mappedStatement, parameter, Executor.NO_RESULT_HANDLER, mappedStatement.getBoundSql());
        List<T> list = executor.query(ms, parameter, RowBounds.DEFAULT, Executor.NO_RESULT_HANDLER, ms.getSqlSource().getBoundSql(parameter));
        return list.get(0);

    }


    @Override
    public Configuration getConfiguration() {
        return configuration;
    }

    @Override
    public <T> T getMapper(Class<T> type) {
        return configuration.getMapper(type, this);
    }
}
